import QtQuick 1.0
import Kookaburra 1.0

Rectangle {
    // Movement properties
    property bool moving   : true
    property int direction : 1
    property int speed     : 3
    property int moveCount      : 0
    property int changeDirection: 20
    property int sightRadius : 300
    property int startPosition : enemy.x

    // Attacking properties
    property int attackDelay: 22
    property bool attacking : false
    property int attackCount: 0
    property bool dead : false;

    // Stats
    property int type       :  1
    property int strength   :  5
    property int defense    :  3
    property int health     :  7

    property string level: ""

    property bool paused: true

    id: enemy

    function enemyTypeOne(){
        strength = (Math.random() * 3) + 3   // 3 - 6
        defense = (Math.random() * 2) + 2    // 2 - 4
        health = (Math.random() * 5) + 4     // 4 - 9
    }

    function enemyTypeTwo(){
        strength = (Math.random() * 5) + 5  // 5 - 10
        defense =( Math.random() * 2) + 2   // 2 - 4
        health = (Math.random() * 6) + 9   //  9 - 15
    }

    function enemyTypeThree(){
        strength = (Math.random() * 5) + 11  // 11 - 16
        defense = (Math.random() * 3) + 3    // 3 - 6
        health = (Math.random() * 11) + 20   //  20 - 31
    }

    onDeadChanged: {
        if (dead) {
            enemy.visible = false;
            respawnTimer.start();
        }
    }

    // Stats
    function createEnemy() {
        switch(type) {
            case 1:
                enemyTypeOne();
                break;
            case 2:
                enemyTypeTwo();
                break;
            case 3:
                enemyTypeThree();
                break;
        }
    }


    width: 60
    height: 80

    color: "blue"

    function enemyMovement() {
        /*
        checkGroundCollision();

        //check object collision
        var obj = world.checkObjectCollision(player);
        if (obj.length > 0) {
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].objectName == "coin") {
                    gameContext.coins++;
                    obj[i].parent.destroy();
                }
            }
        }*/
    }

    function updatePositionX(amount) {
        if(enemy.x + amount < 0) {
            enemy.x = 0;
        } else if (enemy.x + amount > map.width) {
            enemy.x = map.width;
        } else {
            enemy.x += amount;
        }
    }

    CollisionRectangle {
        solid: false
        debug: false

        objectName: "enemy"

        //should be able to use anchors here!
        width: parent.width
        height: parent.height
    }

    /* Movement timer */
    Timer {
        id: movementTimer
        interval: 50
        repeat: true
        running: !paused

        onTriggered: {


            if((player.x >= enemy.x - sightRadius && player.x + player.width - 1 < enemy.x)) {
                direction = -1

                if(attackCount == 0) {
                    attacking = !attacking;
                }

                attackCount = (attackCount + 1) % (attackDelay + 1);

            } else if((player.x - sightRadius <= enemy.x  && player.x - 1 > enemy.x + enemy.width)    )
                {
                direction = 1;


                if(attackCount == 0) {
                    attacking = !attacking;
                }

                attackCount = (attackCount + 1) % (attackDelay + 1);

            } else if (player.x - sightRadius <= enemy.x && player.x - 1 > enemy.x  || player.x >= enemy.x - sightRadius && player.x  < enemy.x ) {
                 direction = 0;
            } else  {
                attacking = false;
                attackCount = 0;
                moveCount = (moveCount + 1) % (changeDirection + 1);
                if(moveCount == changeDirection) {
                    var balance = ((enemy.x >= startPosition + 200) || (enemy.x <= startPosition - 200) ? 1 : 0)
                    direction = Math.random(balance);

                }
            }

            enemyMovement();
            updatePositionX(enemy.direction * enemy.speed)
        }
    }


    Timer {
        id: respawnTimer
        interval: 45000
        repeat: false
        running: false
        onTriggered: {
            createEnemy();
            enemy.dead = false;
            enemy.visible = true;
        }
    }
}
